package com.huasheng.infohub.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.huasheng.infohub.dao.UserInfoDao;
import com.huasheng.infohub.model.UserInfo;

public class UserInfoDaoMysql implements UserInfoDao {

	@Override
	public UserInfo save(UserInfo userinfo) {
		boolean bsuccess = false;
		try {
			Connection conn = null;
			try {
				Context context = new InitialContext();
				DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/info-test");
				conn = ds.getConnection();
				QueryRunner run = new QueryRunner();
				
				run.update(conn, "insert into t_user(user_name, user_password) values (?, ?)", userinfo.getUser(), userinfo.getPassword());
				
				ResultSetHandler<List<Map<String, Object>>> rsh = new MapListHandler();
				List<Map<String,Object>> results = run.query(conn, 
						"SELECT user_id FROM t_user WHERE user_name=?", rsh, userinfo.getUser());
				
				if(results.size()==1) {
					Map<String, Object> map = results.get(0);
					userinfo.setId((Long)map.get("user_id"));
					bsuccess = true;
				} 
			} catch (SQLException e) {
				e.printStackTrace();		
			} catch (NamingException e) {
				e.printStackTrace();
			} finally {
				DbUtils.close(conn);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bsuccess ? userinfo : null;
	}

	@Override
	public UserInfo getUserInfo(String username) {
		UserInfo userinfo = null;
		try {
			Connection conn = null;
			try {
				Context context = new InitialContext();
				DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/info-test");
				conn = ds.getConnection();
				ResultSetHandler<List<Map<String, Object>>> rsh = new MapListHandler();
				QueryRunner qr = new QueryRunner();
				List<Map<String,Object>> results = qr.query(conn, 
						"SELECT user_id, user_name, user_password FROM t_user WHERE user_name=?", rsh, username);
				
				if(results.size()==1) {
					Map<String, Object> map = results.get(0);
					if(map.get("user_name") != null) {
						userinfo = new UserInfo();
						userinfo.setId( (Long)map.get("user_id") );
						userinfo.setUser( (String)map.get("user_name") );
						userinfo.setPassword( (String)map.get("user_password") );
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NamingException e) {
				e.printStackTrace();
			} finally {
				DbUtils.close(conn);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return userinfo;
	}

	@Override
	public boolean update(UserInfo userinfo) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean remove(Integer userid) {
		// TODO Auto-generated method stub
		return false;
	}

}
